﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>监控播放</title>
</head>
<body>
    <div id="divPlugin" style="width:780px;height:580px;">

    </div>

    <script src="~/Assets/js/base/jquery.min.js"></script>
    <script id="videonode" src="~/Assets/js/hkvideo/webVideoCtrl.js"></script>
    <script>
         var id = '@(Request.QueryString["Id"].IsEmpty() ? "" : Request.QueryString["Id"])';
        var ip = '@ViewBag.CModel.IpAddress';
        var port = '@ViewBag.CModel.Port';
        var username =  '@ViewBag.CModel.UserName';
        var password ='@ViewBag.CModel.Password';

        var g_iWndIndex = 0;
        var deviceport = 8000;
        var rtspport = 554;
        //错误码
        //通用错误
        var ERROR_CODE_UNKNOWN = 1000; //未知错误
        var ERROR_CODE_NETWORKERROR = 1001; //网络错误
        var ERROR_CODE_PARAMERROR = 1002; //缺少插件元素

        //登录模块
        var ERROR_CODE_LOGIN_NOLOGIN = 2000; // 未登录
        var ERROR_CODE_LOGIN_REPEATLOGIN = 2001; //设备已登录，重复登录
        var ERROR_CODE_LOGIN_NOSUPPORT = 2002; //当前设备不支持Digest登录

        //预览播放
        var ERROR_CODE_PLAY_PLUGININITFAIL = 3000; //插件初始化失败
        var ERROR_CODE_PLAY_NOREPEATPLAY = 3001; //当前窗口已经在预览
        var ERROR_CODE_PLAY_PLAYBACKABNORMAL = 3002; //回放异常
        var ERROR_CODE_PLAY_PLAYBACKSTOP = 3003; //回放停止
        var ERROR_CODE_PLAY_NOFREESPACE = 3004; //录像过程中，硬盘容量不足

        //对讲
        var ERROR_CODE_TALK_FAIL = 5000; //语音对讲失败


        var version = "V3.3.0build20230322"

        setTimeout(function () {
            load();
        },1000);
        function load() {


            WebVideoCtrl.I_InitPlugin({
                bWndFull: true,     //是否支持单窗口双击全屏，默认支持 true:支持 false:不支持
                iWndowType: 2,
                cbSelWnd: function (xmlDoc) {
                   // g_iWndIndex = parseInt($(xmlDoc).find("SelectWnd").eq(0).text(), 10);
                },
                cbInitPluginComplete: function () {
                    WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(() => {
                        // 检查插件是否最新
                        WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
                            if (bFlag) {
                                alert("检测到新的插件版本，双击开发包目录里的HCWebSDKPlugin.exe升级！");
                            }
                        });
                    }, () => {
                        alert("插件初始化失败，请确认是否已安装插件；如果未安装，请双击开发包目录里的HCWebSDKPlugin.exe安装！");
                        location.href = "../../Assets/js/hkvideo/HCWebSDKPlugin.exe";
                    });
                }
            });
            var szIP = ip,
                szPort =port,
                szUsername =username,
                szPassword =password;

            if ("" == szIP || "" == szPort) {
                return;
            }
            var szDeviceIdentify = szIP + "_" + szPort;
            setTimeout(function () {
                WebVideoCtrl.I_Login(szIP, 1, szPort, szUsername, szPassword, {
                    timeout: 3000,
                    success: function (xmlDoc) {
                        //alert(" 登录成功！");
                        //获取通道
                        getDevicePort(szDeviceIdentify);
						setTimeout(function(){
							WebVideoCtrl.I_GetDigitalChannelInfo(szDeviceIdentify, {
								success: function (xmlDoc) {
									var oChannels = $(xmlDoc).find("InputProxyChannelStatus");
									 var channels = [];
									$.each(oChannels, function (i) {
										var id = $(this).find("id").eq(0).text();
										channels.push(parseInt(id));
									});
									for (var i = 0; i < channels.length; i++) {
                                        g_iWndIndex = i;
										var iRtspPort=rtspport;
                                        var oWndInfo = WebVideoCtrl.I_GetWindowStatus(g_iWndIndex);
                                        var startRealPlay = function () {
                                            WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
                                                iStreamType: 1,
                                                iWndIndex: g_iWndIndex,
                                                iChannelID: channels[i],
                                                bZeroChannel: false,
                                                success: function () {
                                                },
                                                error: function (oError) {
                                                    //alert(szDeviceIdentify + " 开始预览失败！", oError.errorCode, oError.errorMsg);
                                                }
                                            });
                                        };
                                        if (oWndInfo != null) {// 已经在播放了，先停止
                                            WebVideoCtrl.I_Stop({
                                                success: function () {
                                                    startRealPlay();
                                                }
                                            });
                                        } else {
                                            startRealPlay();
                                        }
                                    //开始预览
									}
								},
								error: function (oError) {
									showOPInfo(szDeviceIdentify + " 获取数字通道失败！", oError.errorCode, oError.errorMsg);
								}
							});
						},1000);

                    },
                    error: function (oError) {

                    }
                });

            }, 1000);
        }

        function getDevicePort(szDeviceIdentify) {
            if (null == szDeviceIdentify) {
                return;
            }

            WebVideoCtrl.I_GetDevicePort(szDeviceIdentify).then((oPort) => {
                var deviceport = oPort.iDevicePort;
                var rtspport = oPort.iRtspPort;
            }, (oError) => {
                var szInfo = "获取端口失败！";
                alert(szInfo);
            });
        }
    </script>
</body>
</html>
